﻿using System;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace RuoVea.ExHtml
{
    /// <summary>
    /// html 帮助
    /// </summary>
    public class HtmlHelper
    {
        #region 自动判断编码获取html内容
        /// <summary>
        /// 自动判断编码获取html内容
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string GetHtml(string url)
        {
            string htmlCode;
            HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
            webRequest.Timeout = 30000;
            webRequest.Method = "GET";
            webRequest.UserAgent = "Mozilla/4.0";
            webRequest.Headers.Add("Accept-Encoding", "gzip, deflate");

            HttpWebResponse webResponse = (System.Net.HttpWebResponse)webRequest.GetResponse();

            //获取目标网站的编码格式
            string contentype = webResponse.Headers["Content-Type"];
            Regex regex = new Regex("charset\\s*=\\s*[\\W]?\\s*([\\w-]+)", RegexOptions.IgnoreCase);
            if (webResponse.ContentEncoding.ToLower() == "gzip")//如果使用了GZip则先解压
            {
                using (System.IO.Stream streamReceive = webResponse.GetResponseStream())
                {
                    using (var zipStream = new System.IO.Compression.GZipStream(streamReceive, System.IO.Compression.CompressionMode.Decompress))
                    {
                        //匹配编码格式
                        if (regex.IsMatch(contentype))
                        {
                            Encoding ending = Encoding.GetEncoding(regex.Match(contentype).Groups[1].Value.Trim());
                            using (StreamReader sr = new System.IO.StreamReader(zipStream, ending))
                            {
                                htmlCode = sr.ReadToEnd();
                            }
                        }
                        else
                        {
                            using (StreamReader sr = new System.IO.StreamReader(zipStream, Encoding.UTF8))
                            {
                                htmlCode = sr.ReadToEnd();
                            }
                        }
                    }
                }
            }
            else
            {
                using (System.IO.Stream streamReceive = webResponse.GetResponseStream())
                {
                    using (System.IO.StreamReader sr = new System.IO.StreamReader(streamReceive, Encoding.Default))
                    {
                        htmlCode = sr.ReadToEnd();
                    }
                }
            }
            return htmlCode;
        }
        #endregion

        #region 获取字符中指定标签的值
        /// <summary>  
        /// 获取字符中指定标签的值  
        /// </summary>  
        /// <param name="str">字符串</param>  
        /// <param name="title">标签</param>  
        /// <returns>值</returns>  
        public static string GetTitleContent(string str, string title)
        {
            string tmpStr = string.Format("<{0}[^>]*?>(?<Text>[^<]*)</{1}>", title, title); //获取<title>之间内容  
            Match TitleMatch = Regex.Match(str, tmpStr, RegexOptions.Multiline);
            string result = TitleMatch.Groups["Text"].Value;
            return result;
        }
        #endregion

        #region 获取字符中指定标签的值
        /// <summary>  
        /// 获取字符中指定标签的值  
        /// </summary>  
        /// <param name="str">字符串</param>  
        /// <param name="title">标签</param>  
        /// <param name="attrib">属性名</param>  
        /// <returns>属性</returns>  
        public static string GetTitleContent(string str, string title, string attrib)
        {
            string tmpStr = string.Format("<{0}[^>]*?{1}=(['\"\"]?)(?<url>[^'\"\"\\s>]+)\\1[^>]*>", title, attrib); //获取<title>之间内容  
            Match TitleMatch = Regex.Match(str, tmpStr, RegexOptions.IgnoreCase);
            string result = TitleMatch.Groups["url"].Value;
            return result;
        }
        #endregion

        #region 下载图片
        /// <summary>
        /// 下载图片
        /// </summary>
        /// <param name="imgurl">图片地址</param>
        /// <param name="imgName">图片名称</param>
        /// <param name="Content_pathName">存放地址</param>
        /// <returns></returns>
        public static string DownLoadImg(string imgurl, string imgName, string Content_pathName, string pat = "Img", bool houzhui = false)
        {
            if (string.IsNullOrWhiteSpace(imgurl))
                return null;
            var fileName = imgurl;
            var fileFolder = /*AppDomain.CurrentDomain.BaseDirectory +*/ "D:/Desktop/FilterUpload/" + pat + "/" + Content_pathName + "/";

            if (!Directory.Exists(fileFolder))
                Directory.CreateDirectory(fileFolder);
            try
            {
                WebClient wc = new WebClient();
                string hpuzhi = "";
                int lastInde = fileName.LastIndexOf('.');
                int lenth = fileName.Length;
                string subb = fileName.Substring(lastInde, (lenth - lastInde));

                if (subb == "jpg" || subb == "jpeg")
                {
                    hpuzhi = ".jpg";
                }
                else if (subb == "png")
                {
                    hpuzhi = ".png";
                }
                else if (subb == "gif")
                {
                    hpuzhi = ".gif";
                }

                var path = (houzhui == true ? System.IO.Path.GetFileName(fileName + hpuzhi) : System.IO.Path.GetFileName(fileName));
                if (path.IndexOf('?') > 0)
                    path = path.Substring(0, path.IndexOf('?'));

                wc.DownloadFile(@"" + fileName, fileFolder + path);

                return "/FilterUpload/" + pat + "/" + Content_pathName + "/" + path;
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion

        #region 抓取Html 页面内容
        /// <summary>
        /// 抓取Html 页面内容
        /// </summary>
        /// <returns></returns>
        public static string GetHtmlContent(string url, string encod = "utf-8")
        {
            if (string.IsNullOrEmpty(url))
            {
                return "";
            }
            try
            {
                //创建一个请求
                HttpWebRequest httprequst = (HttpWebRequest)WebRequest.Create(url);
                //不建立持久性链接
                httprequst.KeepAlive = true;
                //设置请求的方法
                httprequst.Method = "GET";
                //设置标头值
                httprequst.UserAgent = "User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705";
                //httprequst.UserAgent = "User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1";
                //httprequst.UserAgent = "User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0";
                //httprequst.UserAgent = "User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50";
                //httprequst.UserAgent = "User-Agent:Opera/9.80 (Windows NT 6.1; U; zh-cn) Presto/2.9.168 Version/11.50";
                //httprequst.UserAgent = "User-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; Tablet PC 2.0; .NET4.0E)";
                //httprequst.UserAgent = "User-Agent:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; InfoPath.3)";
                //httprequst.UserAgent = "User-Agent:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB7.0)";
                //httprequst.UserAgent = "User-Agent:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";
                //httprequst.UserAgent = "User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)";
                //httprequst.UserAgent = "User-Agent:Mozilla/5.0 (Windows; U; Windows NT 6.1; ) AppleWebKit/534.12 (KHTML, like Gecko) Maxthon/3.0 Safari/534.12";
                //httprequst.UserAgent = "User-Agent:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E)";

                httprequst.Accept = "*/*";
                httprequst.Headers.Add("Accept-Language", "zh-cn,en-us;q=0.5");
                httprequst.ServicePoint.Expect100Continue = false;
                httprequst.Timeout = 5000;
                httprequst.AllowAutoRedirect = true;//是否允许302
                ServicePointManager.DefaultConnectionLimit = 30;
                //获取响应
                HttpWebResponse webRes = (HttpWebResponse)httprequst.GetResponse();
                //获取响应的文本流
                string content = string.Empty;
                using (System.IO.Stream stream = webRes.GetResponseStream())
                {
                    using (System.IO.StreamReader reader = new StreamReader(stream, System.Text.Encoding.GetEncoding(encod)))
                    {
                        content = reader.ReadToEnd();
                    }
                }
                //取消请求
                httprequst.Abort();
                //返回数据内容
                return content;
            }
            catch (Exception ex)
            {
                var rt = ex;
                return "";
            }
        }

        #endregion

        #region 抓取Html 页面内容
        ///<summary>
        ///采用https协议访问网络
        ///</summary>
        ///<param name="URL">url地址</param>
        ///<param name="strPostdata">发送的数据</param>
        ///<returns></returns>
        public static string PostHtmlContent(string URL, string strPostdata, string strEncoding = "gbk")
        {
            if (string.IsNullOrEmpty(URL))
            {
                return "";
            }
            try
            {
                Encoding encoding = Encoding.Default;
                HttpWebRequest httprequst = (HttpWebRequest)WebRequest.Create(URL);
                httprequst.Method = "post";
                httprequst.Accept = "text/html, application/xhtml+xml, */*";
                httprequst.ContentType = "application/x-www-form-urlencoded";
                //设置标头值
                httprequst.UserAgent = "User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705";
                httprequst.Accept = "*/*";
                httprequst.Headers.Add("Accept-Language", "zh-cn,en-us;q=0.5");
                httprequst.ServicePoint.Expect100Continue = false;
                httprequst.Timeout = 5000;
                httprequst.AllowAutoRedirect = true;//是否允许302
                ServicePointManager.DefaultConnectionLimit = 30;

                byte[] buffer = encoding.GetBytes(strPostdata);
                httprequst.ContentLength = buffer.Length;
                httprequst.GetRequestStream().Write(buffer, 0, buffer.Length);
                HttpWebResponse response = (HttpWebResponse)httprequst.GetResponse();
                //获取响应的文本流
                string content = string.Empty;
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding(strEncoding)))
                {
                    content = reader.ReadToEnd();
                }
                //取消请求
                httprequst.Abort();
                //返回数据内容
                return content;
            }
            catch (Exception)
            {

                return "";
            }
        }
        #endregion
    }
}
